<?php
    require APPPATH .'third_party/admin_controller'.EXT;
    class News_Product extends Admin_Controller
    {
        public function __construct()
        {
            parent::__construct();
        }
        public function index()
        {
            $this->check_login_admin();
            $data = array();
           // $data['show'] = $this->news_product_model->show();
            $config['base_url'] =  base_url().'admin/news_product/index/';
			$uid =$this->uri->segment('4');            
			$config['per_page'] = '20'; 				
			$config['uri_segment'] = 4;
			//lay tong so trang
			$config['total_rows'] = $this->news_product_model->total();				
			$this->pagination->initialize($config);
			$data['page'] = $this->pagination->create_links();
			
			$data['show'] = $this->news_product_model->news_product_phantrang($uid,$config['per_page']);
            $this->load_render($this->load->view('news_product/show',$data,TRUE));
        }
        public function add_update()
        {
            $this->check_login_admin();
            $data = array();
			$data['show_news'] = $this->news_cat_model->show();
            $valid = $this->form_validation;
            $valid->set_rules('txt_title','Title','trim|required|xss_clean');
            $valid->set_rules('txt_order','order','trim|required|xss_clean');           
            
            $valid->set_rules('txt_author','Author','trim|required|xss_clean');
            $valid->set_rules('txt_body','body','trim|required|xss_clean');
            $valid->set_rules('txt_id_news','Name Cats','trim|required|xss_clean');
            $valid->set_rules('txt_meta_keyword','Meta Keyword','trim|required|xss_clean');
            $valid->set_rules('txt_meta_description','Meta Desctiption','trim|required|xss_clean');
			$valid->set_rules('txt_title_short','Nội dung ngắn','trim|required|xss_clean');
           
            $id = $this->uri->segment('4');
			$image = $this->input->post('image');
			$image_thumb = $this->input->post('image_thumb');
            if($valid->run() == TRUE)
            {
				$data = array(
                'title' =>$this->input->post('txt_title'),
                'author'=>$this->input->post('txt_author'),
                'date_post'=>date('Y-m-d H:i:s'),
                'body'    =>$this->input->post('txt_body'),               
                'active'  =>$this->input->post('txt_active'),
                'order_news_product'   =>$this->input->post('txt_order'),
                'id_news' =>$this->input->post('txt_id_news'),
                'url'     =>$this->util->alias($this->input->post('txt_title')),             
                'meta_keyword' =>$this->input->post('txt_meta_keyword'),
                'meta_description' =>$this->input->post('txt_meta_description'),
                'tags' =>$this->input->post('txt_tag'),
				'vitri_show' =>$this->input->post('txt_vt_active'),
				'title_short' =>($this->input->post('txt_title_short'))
				); 
				if($_FILES['p_image']['name']!='')
				{
					$config['upload_path'] = './upload/news/';
					$config['allowed_types'] = 'gif|jpg|png';
					$config['remove_spaces'] = TRUE;
					$config['max_size'] = 1024;
					$config['max_width'] = '1024';
					$config['max_height'] = '1024';
					$config['file_name'] = $_FILES['p_image']['name'];
					$this->load->library('upload', $config);
					if(!$this->upload->do_upload('p_image')) 
					{
					return false;
					} else {
					$upload = $this->upload->data();
					$data['image'] = 'upload/news/'.$upload['file_name'];
					$data['image_thumb'] = 'upload/news/'.$upload['raw_name'].'_thumb'.$upload['file_ext'];
					// tao image thumb
						$config['image_library'] = 'gd2';
						$config['source_image'] = $upload['full_path'];
						$config['thumb_maker'] = '_thumb';
						$config['create_thumb'] = TRUE;
						$config['maintain_ratio'] = FALSE;
						$config['quality'] = '100%';
						$config['width'] = '100';
						$config['height'] = '120';
						$config['new_image'] = $upload['file_name'];	
						$this->load->library('image_lib', $config);
						$this->image_lib->resize();
						$this->image_lib->clear();
						if($image !='' && $image_thumb !='')
						{
						@unlink($image);
						@unlink($image_thumb);
						}
					
						}	
				}
				else
				{
					if($image !='' && $image_thumb !='') {
					$data['image'] = $image;
					$data['image_thumb'] = $image_thumb;
					} else {
						$data['image'] = '';
						$data['image_thumb'] = '';
					}
				}
				
                
                if($id !='')
                {
                    $this->news_product_model->update_news_product($id,$data);
                    $data['error'] = "Cap nhat thanh cong";
                }
                else
                {
                    $this->news_product_model->add_news_product($data);
                    $data['error'] = "Them thanh cong";
                }
                redirect('admin/news_product/');
            }
			$data['uid'] = $id;
            $data['show'] = $this->news_product_model->get_id($id);
            $this->load_render($this->load->view('news_product/add',$data,TRUE));
            
        }
        public function delete_news()
        {
            $this->check_login_admin();
            $data = array();
            $id = $this->uri->segment('4');
            if($this->news_product_model->delete_id($id))
            {
                redirect('admin/news_product/');
            }
        }
        public function change_update_active()
        {
            $this->check_login_admin();
            $data = array();
            $id = $this->input->get('id');
            $data['active'] = $this->input->get('active');
            if($this->news_product_model->change_update_active($id,$data))
            {
                $data['error'] = "Cập nhật thành công";
            }
            echo json_encode($data);
        }
    }
?>